﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JWXH.XH.pages.listview.viewmodel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace JWXH.XH.pages.listview
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ListViewPages : ContentPage
    {
        private ObservableCollection<FlyWall> listData = new ObservableCollection<FlyWall>();
        private int pageIndex = 1;
        private bool isLoading = false;
        public ListViewPages()
        {
            InitializeComponent();
            ListViewData.ItemsSource = listData;
            InitData();
        }

        public async Task InitData()
        {
            if (isLoading) return;
            isLoading = true;
            var list = await GetList(pageIndex);
            if (list.Count < 5)
            {
                this.FooterText.Text = "没有更多了";
            }
            else
            {
                this.FooterText.Text = "加载中。。。";
                if (pageIndex <= 1) listData.Clear();
                foreach (var item in list)
                {
                    listData.Add(item);
                }
            }
            pageIndex++;
            isLoading = false;
        }

        public async Task<List<FlyWall>> GetList(int pageIndex)
        {
            await Task.Delay(2000);
            var ret = new List<FlyWall>();
            int count = pageIndex > 2 ? 0 : 20;
            for (int i = 1; i < count / 3; i++)
            {
                ret.AddRange(new List<FlyWall>()
                {
                    new FlyWall()
                    {
                        Name = "shadowsocks" + pageIndex,
                        Desc = "first fly wall tool",
                        Img = "http://jwell-xh.oss-cn-shenzhen.aliyuncs.com/20180226093621_ac3530503ab64bc0a30546b7b98af417_.jpg?x-oss-process=image/resize,h_224,w_280"
                    },new FlyWall()
                    {
                        Name = "shadowsocksR" + pageIndex,
                        Desc = "more speed fly wall tools",
                        Img = "http://www.52hhx.com/imgs/header_21ebbfd7.png"
                    },new FlyWall()
                    {
                        Name = "v2ray" + pageIndex,
                        Desc = "more safe and speed",
                        Img = "http://jwell-xh.oss-cn-shenzhen.aliyuncs.com/20180223135656_f70a80b8e4694dc889b7ec0a383e788d_.jpg?x-oss-process=image/resize,h_224,w_280"
                    }
                });
            }
            return ret;
        }

        private void ListViewData_OnItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
            if (listData?.Count > 0)
            {
                if (listData[listData.Count - 1] == e.Item)
                {
                    InitData();
                }
            }
        }

        private async void ListViewData_OnRefreshing(object sender, EventArgs e)
        {
            pageIndex = 1;
            await InitData();
            ListViewData.IsRefreshing = false;
        }
    }
}